package com.mediacloud.app.newsmodule.fragment.recommend;

import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.mediacloud.app.assembly.util.Utility;
import com.mediacloud.app.assembly.widget.LinearListView;
import com.mediacloud.app.model.interfaces.DataInvokeCallBack;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.CMSFieldStyle;
import com.mediacloud.app.model.news.CatalogItem;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.adaptor.NewsListItemStyleAdaptor;
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils;
import com.mediacloud.app.newsmodule.utils.RelateArticleDataInvoker;

import java.util.List;

/**
 * 相关推荐
 * Created by mediacloud.app on 2016/12/27.
 * 需要传入 catalogItem
 */

public class RelatedRecommendationListFragment extends Fragment {

    protected final String TAG = getClass().getName();
    protected Bundle createBundle;

    CatalogItem catalogItem;
    ArticleItem articleItem;

    protected ViewGroup mRootView;

    protected LinearListView articleListView;
    NewsListItemStyleAdaptor articlesAdapter;

    RelateArticledDataCallBack relateArticledDataCallBack;
    RelateArticleDataInvoker relateArticleDataInvoker;
    View qingdao_separate;

    public RelatedRecommendationListFragment() {
        Log.w(TAG, this + "newInstance");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = (ViewGroup) inflater.inflate(getLayoutResID(), null);
        mRootView.setBackgroundColor(Color.WHITE);
        mRootView.setVisibility(View.GONE);
        createBundle = getArguments();
        catalogItem = createBundle.getParcelable("catalog");
        articleItem = createBundle.getParcelable("data");
        initView();
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        relateArticledDataCallBack = new RelateArticledDataCallBack();
        relateArticleDataInvoker = new RelateArticleDataInvoker(relateArticledDataCallBack);
        relateArticleDataInvoker.getArticleListById(String.valueOf(articleItem.getId()), 1, 20);
    }

    @Override
    public void onDestroy() {
        if (relateArticleDataInvoker != null)
            relateArticleDataInvoker.destory();
        relateArticleDataInvoker = null;
        super.onDestroy();
    }

    private void initView() {
        articleListView = Utility.findViewById(mRootView, R.id.articleListView);
        qingdao_separate = Utility.findViewById(mRootView, R.id.qingdao_separate);
        if (getResources().getString(R.string.tenantid).equals(getResources().getString(R.string.tenant_qingdao_lanjing)))
            qingdao_separate.setVisibility(View.GONE);
    }

    public int getLayoutResID() {
        return R.layout.normalnews_content_related_recommendation;
    }

    CMSFieldStyle cmsFieldStyle = new CMSFieldStyle();

    //相关文章
    class RelateArticledDataCallBack implements DataInvokeCallBack<RelateArticleDataInvoker.RelateArticleDataReciver> {

        @Override
        public void success(RelateArticleDataInvoker.RelateArticleDataReciver data) {
            List<ArticleItem> listArticles = data.listArticles;
            if (listArticles == null || listArticles.size() == 0) {
                mRootView.setVisibility(View.GONE);
                return;
            }
            for (ArticleItem item : listArticles) {
                item.setCmsCustomStyle(cmsFieldStyle);//去掉cms样式
            }
            mRootView.setVisibility(View.VISIBLE);
            final CatalogItem cat = new CatalogItem();
//            cat.setCatalog_type(catalogItem.getCatalog_type());
            cat.setCatid(catalogItem.getCatid());
            cat.setCatname(catalogItem.getCatname());
            articlesAdapter = new NewsListItemStyleAdaptor(getActivity(), cat);
            articlesAdapter.setListContentData(listArticles);
            articleListView.setAdapter(articlesAdapter);
            articleListView.setOnItemClickListener(new LinearListView.OnItemClickListener() {
                                                       @Override
                                                       public void onItemClick(LinearListView parent, View view, int position, long id) {
                                                           ArticleItem article = articlesAdapter.getItem(position);
                                                           NewsItemClickUtils.OpenItemNewsHandle(getActivity(), article.getType(), article, articlesAdapter.catalogItem);
                                                       }
                                                   }
            );
        }

        @Override
        public void fault(Object data) {
            mRootView.setVisibility(View.GONE);
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (articlesAdapter != null) {
            articlesAdapter.destroy();
        }
    }
}